const { outputFileSync, readFileSync } = require('fs-extra')
const path = require('path')
const template = require('art-template')
const dayjs = require('dayjs')

module.exports = (postName, layout = 'post') => {
  if (!postName.endsWith('.md')) {
    postName = `${postName}.md`
  }
  if (!layout.endsWith('.md')) {
    layout = `${layout}.md`
  }
  const data = template(
    path.join(process.cwd(), './scaffolds', layout),
    {
      title: path.parse(postName).name,
      date: dayjs().format('YYYY-MM-DD'),
      updated: dayjs().format('YYYY-MM-DD')
    }
  )
  outputFileSync(
    path.join('./_posts/', postName),
    data
  )
}
